package com.scp.modelo;

import java.util.ArrayList;
import java.util.Date;

import android.content.ContentValues;
import android.database.Cursor;

import com.scp.util.Aplicacao;
import com.scp.util.Util;

public class Registro extends ModeloBase {
	
	private Date data;
	private Date tempo;	
	
	public Date getData() {
		return data;
	}
	public void setData(Date data) {
		this.data = data;
	}
	public Date getTempo() {
		return tempo;
	}
	public void setTempo(Date tempo) {
		this.tempo = tempo;
	}
		
	public Registro() {
		this.setNomeTabela("Registros");
		this.setColunas(new String[] {"_id","Data", "Tempo"});
	}
	
	public boolean Gravar() {
		ContentValues values = new ContentValues();
		values.put("Data", Util.dateToString(this.getData(), "yyyy-MM-dd"));
		values.put("Tempo", Util.dateToString(this.getTempo(), "HH:mm:ss"));		
		
		this.set_id(Aplicacao.getDB().Gravar(this.getNomeTabela(), values, "_id=?", new String[] {String.valueOf(this.get_id())}));
		
		return (this.get_id() > 0);
	}
	
	public void BuscarPorPk() {
		Cursor c = Aplicacao.getDB().Buscar(this.getNomeTabela(), this.getColunas(), "_id=?", new String[] {String.valueOf(this.get_id())});
		try {
			c.moveToFirst();
			
			this.setData(Util.stringToDate(c.getString(c.getColumnIndex("Data"))));
			this.setTempo(Util.stringToDate(c.getString(c.getColumnIndex("Tempo")), "HH:mm:ss"));
		} finally {
			c.close();
		}
	}
	
	public ArrayList<Registro> BuscarPorData(Date data)  {
		ArrayList<Registro> registros = new ArrayList<Registro>();
		
		Cursor c = Aplicacao.getDB().Buscar(this.getNomeTabela(), this.getColunas(), "Data=?", new String[] {Util.dateToString(data, "yyyy-MM-dd")}, "Tempo");
		try {
			while (c.moveToNext()) {
				Registro registro = new Registro();
				registro.set_id(c.getLong(c.getColumnIndex("_id")));
				registro.setData(Util.stringToDate(c.getString(c.getColumnIndex("Data")), "yyyy-MM-dd"));
				registro.setTempo(Util.stringToDate(c.getString(c.getColumnIndex("Tempo")), "HH:mm:ss"));
				registros.add(registro);
			}
		} finally {
			c.close();
		}
		
		return registros;
	}	
}
